package com.github.chirspan.xaas.auth.endpoint;

import com.github.chirspan.xaas.core.rest.RestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author  ChenPan
 * @date   2018/7/31 15:18
 * description:
 */
@RestController
public class RevokeTokenEndpoint {

    @Autowired
    @Qualifier("tokenServices")
    DefaultTokenServices tokenServices;


    @GetMapping("/oauth/remove_token")
    public RestResult revokeToken(HttpServletRequest request) {
        String authorization = request.getHeader("Authorization");
        if (authorization != null && authorization.contains("Bearer")) {
            String tokenId = authorization.substring("Bearer".length() + 1);

            if (tokenServices.revokeToken(tokenId)) {

                return    RestResult.OK(null,"成功");
            }
        }
        return RestResult.fail(null,"失败");
    }
}
